<template>
    <div class="address-index  " >
        <div class="plr15 list ">
            <ClList 
                :loading="loading"
                :finished="finished"
                finishedText="没有更多了"
                @load="ListPassenger"
                :list="list">
                <div>
                    <div class="item ptb15 fs-14" v-for="(item, i) in list" :key="i">
                        <div class="flex1">
                            <div class="">
                                <span>杨先生</span>
                                <span class="ml30">1800000000</span>
                            </div>
                            <div class="c-999 pt10">安慰撒大声地</div>
                            <div class="pt10 c-37af74 "><van-icon class="v-middle" name="checked" /><span class="v-middle">默认地址</span></div>
                        </div>
                        <div class="fs-18 pr20">
                            <van-icon name="edit" />
                        </div>
                    </div>
                </div>
            </ClList>
        </div>

        <div class="add-btn">
            <div class="add-btn-inner">
                <van-button type="default" block round color="#37af74">添加地址</van-button>
            </div>
        </div>
    </div>
</template>

<script>
import passengerApi from "@/api/api-passenger";
import ClList from "@/components/ClList"
export default {
    name: "addressIndex",
    components: {
        ClList
    },
    data(){
        return {
            list: [{},  {}],
            loading: false,
            finished: false,
            pageIndex: 1,
            pageSize: 20,
            sel: 0
        }
    },
    onLoad(option){
        this.sel = parseInt(option.use)
    },
    mounted() {
        // this.ListPassenger()
    },
    onShow(){
        if(wx.getStorageSync("refresh-address") == 1){
            this.ListPassenger(true)
        }
        wx.removeStorageSync("refresh-address")
    },
    methods: {
        ListPassenger(isReload){
            var vm = this
            this.loading = true
            if(isReload){
                this.pageIndex = 1
                this.finished = false
            }
            
            passengerApi.ListPassenger({
                pageIndex: this.pageIndex,
                pageSize: this.pageSize
            }).then(res => {
                res = res.result
                this.finished = res.IsLastPage
                this.loading = false
                if(res.List){
                    res.List.forEach(v => {
                        v.mobileBak = this.resetMobil(v.mobile)
                        v.idcardBak = this.resetIdcard(v.idcard)
                        v.sel = vm.sel
                    });
                }
                if(isReload){
                    this.list = res.List || []
                } else {
                    this.list = this.list.concat(res.List || [])
                }
            })
        },
        resetMobil(mobile){
            return (mobile.substring(0, 3) + '****' + mobile.substring(7, 11))
        },
        resetIdcard(idcard){
            return (idcard.substring(0, 5) + '*********' + idcard.substring(13))
        },
        UpdatePassenger(item, isDefault){
            var vm = this
            item.is_default = isDefault
            passengerApi.UpdatePassenger(item).then(res => {
                this.ListPassenger(true)
                this.$toast('设置成功')
            })
        },
        DeletePassenger(item){
            var vm = this
            wx.showModal({
                title: '提示',
                content: '确认删除吗？',
                success (res) {
                    if (res.confirm) {
                        passengerApi.DeletePassenger({passengerId: item.passengerId}).then(res => {
                            vm.ListPassenger(true)
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success',
                                duration: 2000
                            })
                        })
                    } else if (res.cancel) {
                    
                    }
                }
            })
        },
        toUpdate(item){
            wx.navigateTo({
                url: "/pages/personal/address/add?id="+ (item? item.passengerId : '')
            })
        },
        useAddress(item){
            wx.setStorageSync("sc-use-address", JSON.stringify(item))
            wx.navigateBack()
        }
    }
};
</script>
<style lang="scss">
@import './css/index';
</style>
